package cn.g31010.sip.component
{
	import flash.display.Sprite;
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.Responder;

  public class Deux extends Sprite
  {
        private var nc:NetConnection;
        private var good:Boolean;
        private var netOut:NetStream;
        private var netIn:NetStream;
        private var cam:Camera;
        private var mic:Microphone;
        private var responder:Responder;
        private var vidOut:Video;
        private var vidIn:Video;
        private var outStream:String;
        private var inStream:String;
 

        public function Deux (rtmpurl:String)
        {
             var rtmpNow:String=rtmpurl;
             nc=new NetConnection;
             nc.connect (rtmpNow);
             nc.addEventListener (NetStatusEvent.NET_STATUS,getStream);
        }

 

        private function getStream (e:NetStatusEvent):void
        {
             good=e.info.code == "NetConnection.Connect.Success";
             if (good)
             {
                   responder=new Responder(streamNow);
                   nc.call ("streamSelect",responder);
             }
        }

 

        private function streamNow (streamSelect:String):void
        {
             setCam ();
             setMic ();
             setVid ();
             switch (streamSelect)
             {
                   case "left" :
                        outStream="left";
                        inStream="right";
                        break;
                   case "right" :
                        outStream="right";
                        inStream="left";
                        break;
             }

             //Publish local video
             netOut=new NetStream(nc);
             netOut.attachAudio (mic);
             netOut.attachCamera (cam);
             vidOut.attachCamera (cam);
             netOut.publish (outStream, "live");
             //Play streamed video
             netIn=new NetStream(nc);
             vidIn.attachNetStream (netIn);
             netIn.play (inStream);
        }

 

        private function setCam ():void
        {
             cam=Camera.getCamera();
             cam.setMode (240,180,15);
             cam.setQuality (0,85);
        }

        private function setMic ():void
        {
             mic=Microphone.getMicrophone();
             mic.rate=11;
             mic.setSilenceLevel (12,2000);
        }

        private function setVid ():void
        {
             vidOut=new Video(240,180);
             addChild (vidOut);
             vidOut.x=25;
             vidOut.y=110;
             vidIn=new Video(240,180);
             addChild (vidIn);
             vidIn.x=vidOut.x+260;
             vidIn.y=110;
        }
  }
}